
export interface NotificationOptions {
  body?: string;
  icon?: string;
  vibrate?: number[];
  data?: { url: string };
}


    let notificationPermission: NotificationPermission='default';
   
    // 请求通知权限
 export const requestNotificationPermission = async () => {
        if (typeof window !== 'undefined' && Notification.permission !== 'granted') {
            const permission = await Notification.requestPermission();
            notificationPermission=permission;
            return permission === 'granted';
        }
        return Notification.permission === 'granted';
    };

    // 显示通知
    const showNotification = (title: string, options?: NotificationOptions) => {
        if (Notification.permission === 'granted') {
            // 检查浏览器是否支持Notification
            if ('Notification' in window) {
                new Notification(title, options);
            }
        }
    };

    // 点击按钮触发通知
   export const handleNotifyClick = async (message: string) => {
        // 先检查权限
        const hasPermission = await requestNotificationPermission();

        if (hasPermission) {
            showNotification('这是一条通知', {
                body: `${message}`,
                icon: '/favicon.ico', // 可选，通知图标
                vibrate: [200, 100, 200], // 可选，振动模式（仅移动设备）
                data: { url: '/home' } // 可选，附加数据
            });
        } else {
            alert('请允许通知权限才能接收通知');
        }
    };




